<?php

namespace App\Exports;

use App\Models\User;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
//use Maatwebsite\Excel\Concerns\WithEvents;
//use Maatwebsite\Excel\Events\AfterSheet;
//
//use Maatwebsite\Excel\Concerns\ShouldAutoSize;
//use Maatwebsite\Excel\Concerns\WithStrictNullComparison;

class NormalExport implements FromCollection, WithColumnWidths
{
    public $row;
    public $data;
    /**    public $data;
    /**
    * @return \Illuminate\Support\Collection
    */
    public function __construct($row,$data)
    {
        $this->row = $row;
        $this->data = $data;
    }

//    public function registerEvents(): array
//    {
//        return [
//            AfterSheet::class  => function(AfterSheet $event) {
//
////                $event->sheet->getDelegate()->getColumnDimension('A')->setWidth(20);
////                $event->sheet->getDelegate()->getColumnDimension('B')->setWidth(20);
////                $event->sheet->getDelegate()->getColumnDimension('C')->setWidth(20);
////                $event->sheet->getDelegate()->getColumnDimension('D')->setWidth(20);
////                $event->sheet->getDelegate()->getColumnDimension('E')->setWidth(100);
////                $event->sheet->getDelegate()->getColumnDimension('F')->setWidth(50);
//
//                //设置区域单元格垂直居中
//                $event->sheet->getDelegate()->getStyle('A1:Z1265')->getAlignment()->setVertical('center');
//                //设置区域单元格水平居中
//                $event->sheet->getDelegate()->getStyle('A1:Z1265')->getAlignment()->setHorizontal('center');
//                //设置行高，$i为数据行数
//                //设置区域单元格垂直居中
//
//            }
//        ];
//    }

    public function collection()
    {
        $row = $this->row;
        $data = $this->data;
        //设置表头
        foreach ($row[0] as $key => $value) {
            $key_arr[] = $key;
        }
        //输入数据
        foreach ($data as $key => &$value) {
            $js = [];
            for ($i=0; $i < count($key_arr); $i++) {
                $js = array_merge($js,[ $key_arr[$i] => $value[ $key_arr[$i] ] ]);
            }
            array_push($row, $js);
            unset($val);
        }
        return collect($row);
    }


    public function columnWidths(): array
    {
        return [
            'A' => 50,
            'B'=>10
        ];
    }
}
